<?php

class Isystem extends MY_Controller {

    private static $url = 'http://houtai.5664wan.com/isdk/';
    private static $key = "M1AA1UOTTQPW";

    public function __construct() {

        parent::__construct();
         $this->load->model('comm_model');
          $this->load->library('encrypt');
    }

    public function index() {

        $this->load->model('product_model', 'pm');

        $data = array('pid' => 1);

        $re = array();
        $re['makes'] = $this->pm->get_main_makes();
        $re['smake'] = $this->pm->get_main_makes(0, 12);
        $re['proinfo'] = $this->pm->get_products_by_limit('', '', '', 10, 6);
        foreach ($re['proinfo'] as $k => $v) {

            $proimg = $this->pm->get_img($v->pro_id, true);
            $re['proinfo'][$k]->img = $proimg->img;
        }

        $this->_header($_SERVER['HTTP_HOST'], 'index', $re);
    }

    public function leftajax() {
        $this->load->view('leftsidebar_ajax');
    }

    public function login() {

        $data = array();

        if ($loginerror = array_key_exists('loginerror', $this->session->all_userdata()) ? $this->session->userdata('loginerror') : false) {

            $data['loginerror'] = $loginerror;

            $this->session->unset_userdata('loginerror');
        } else {

            $data['loginerror'] = '';
        }

        $this->_header('login', 'login', $data);
    }
    public function get_key() {
         $this->load->library('myclass');
        $site_info = $this->comm_model->get_siteinfo();

        $site_id = $site_info->siteid;


        $get_key = $this->myclass->get_key($site_info->siteid);
      
         
        return $get_key;
    }
    public function check() {

        $this->load->library('myclass');

        $register_ip = $this->myclass->get_onlineip();

        $this->load->model('comm_model');

        $site_info = $this->comm_model->get_siteinfo();

        $data = $this->input->post();

        $register_info = array(
            'siteid' => $site_info->siteid,
            'email' => $data['email'],
            'password' => md5($data['password'])
        );

        $get_key = $this->myclass->get_key($site_info->siteid);

        $this->load->library('encrypt');

        $the_data = $this->encrypt->encode(json_encode($register_info), $get_key);

        $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;

        $httpCode = '';

        $res = $this->myclass->vpost(self::$url . "user_login", $send_info, $httpCode);

        if ($httpCode['http_code'] == 200) {
            $result = json_decode($res);
            // var_dump($result);die(0);
            switch ($result->ack) {
                case 0:
                    $this->session->set_userdata('userinfo', $result);
                    
                    if ($repage = array_key_exists('repage', $this->session->all_userdata()) ? $this->session->userdata('repage') : false) {

                        $this->session->unset_userdata('repage');
                        header("location:" . $repage);
                        exit;
                    } else {

                        //$this->_header('user_mag','myorder',$data);
                        header("location:/");
                        exit;
                    }
                    break;
                case -1:
                    $this->session->set_userdata('loginerror', 'email or password error!');
                    header("location:login");
                    break;
                default:
                    $this->session->set_userdata('loginerror', 'login fail!');
                    header("location:login");
            }
        }
    }

    public function sign_out() {

        $this->session->unset_userdata('userinfo');

        echo "<script>location.href='/';</script>";

        exit;
    }

    public function register() {

        if ($registerror = array_key_exists('registerror', $this->session->all_userdata()) ? $this->session->userdata('registerror') : false) {

            $data['registerror'] = $registerror;

            $this->session->unset_userdata('registerror');
        } else {

            $data['registerror'] = '';
        }

        $this->_header('register', 'register', $data);
    }

    public function recheck() {

        $this->load->library('myclass');

        $register_ip = $this->myclass->get_onlineip();

        $this->load->model('comm_model');

        $site_info = $this->comm_model->get_siteinfo();

        $data = $this->input->post();

        $shipaddr = array(
            'firstname' => $data['firstname'],
            'lastname' => $data['lastname'],
            'address' => '',
            'city' => '',
            'state' => '',
            'postcode' => '',
            'country' => '',
            'phone' => ''
        );
        $billaddr = array(
            'firstname' => $data['firstname'],
            'lastname' => $data['lastname'],
            'address' => '',
            'city' => '',
            'state' => '',
            'postcode' => '',
            'country' => '',
            'phone' => ''
        );

        $register = array(
            'email' => $data['email'],
            'password' => md5($data['password']),
            'firstname' => $data['firstname'],
            'lastname' => $data['lastname'],
            'ip' => $register_ip,
            'siteid' => $site_info->siteid,
            'shipaddr' => serialize($shipaddr),
            'billaddr' => serialize($billaddr)
        );

        $get_key = $this->myclass->get_key($site_info->siteid);

        $this->load->library('encrypt');

        $the_data = $this->encrypt->encode(json_encode($register), $get_key);

        $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;

        $httpCode = '';

        $res = $this->myclass->vpost(self::$url . "register_user", $send_info, $httpCode);

        if ($httpCode['http_code'] == 200) {
            $result = json_decode($res);
            switch ($result->ack) {
                case 0:
                    $this->session->set_userdata('userinfo', $result);
                    if ($repage = array_key_exists('repage', $this->session->all_userdata()) ? $this->session->userdata('repage') : false) {

                        $this->session->unset_userdata('repage');
                        header("location:" . $repage);
                        exit;
                    } else {
                         header("location:/");
                        exit;
                    }
                    break;
                case -1:
                    $this->session->set_userdata('registerror', 'Email already exists!');
                    header("location:login");
                    break;
                default:
                    $this->session->set_userdata('registerror', 'regist fail!');
                    header("location:login");
            }
        }
    }

    public function get_make() {

        $this->load->model('product_model');

        $makes = $this->product_model->get_products_by_limit($this->input->post(), 1, 'main_make_id');

        $make_name = array();

        foreach ($makes as $k => $v) {

            $q = $this->product_model->get_make_name($v->main_make_id);

            $make_name[$v->main_make_id] = $q->make_name;
        }

        if ($make_name) {

            echo '<select name="main_make_id" id=""  tabindex="1" onchange="get_model(this.value,' . $this->input->post('year') . ')">';

            foreach ($make_name as $k => $v) {

                echo '<option value="' . $k . '">' . $v . '</option>';
            }

            echo '</select>';
        } else {

            echo 0;
        }
    }

    public function get_model() {

        $this->load->model('product_model');

        $models = $this->product_model->get_products_by_limit($this->input->post(), 1, 'model');

        if ($models) {

            echo '<select name="model" id=""  tabindex="1">';

            foreach ($models as $k => $v) {

                echo '<option value="' . $v->model . '">' . $v->model . '</option>';
            }

            echo '</select>';
        } else {

            echo 0;
        }
    }

    public function partsearch() {

        if ($datai = $this->input->post() ? $this->input->post() : 'fhsisi') {

            $this->session->set_userdata('psinfo', $datai);
        }

        $this->load->library('myclass');

        $curr_page = $this->uri->segment(2) ? $this->uri->segment(2) : 1;

        $this->load->model('product_model', 'pm');

        $total_page = $this->pm->get_total_nums('products', $this->session->userdata('psinfo'));
        $size = 9;

        $proinfo = $this->pm->get_info('products', $this->session->userdata('psinfo'), $curr_page - 1, $size);

        foreach ($proinfo as $k => $v) {

            $proimg = $this->pm->get_img($v->pro_id, true);
            $proinfo[$k]->img = $proimg->img;
        }

        $pagination = $this->myclass->pagination($curr_page, $total_page / $size, '/partsearch');

        //var_dump($proinfo);exit;

        $this->_header('', 'search', $proinfo, 0, $pagination);
    }

    public function product() {

        $prourl = $this->uri->segment(2);

        $this->load->model('product_model', 'pm');

        $re = $this->pm->get_info_row('products', array('prourl' => $prourl));

        $proimgs = $this->pm->get_img($re->pro_id);

        $imgs = array();

        foreach ($proimgs as $k => $v) {

            $imgs[] = $v->img;
        }

        $re->img = $imgs;

        $this->_header($re->proname, 'product_view', $re);
    }

    public function add_to_cart() {

        if ($proid = $this->input->get_post('id') ? $this->input->get_post('id') : '') {

            $this->load->model('cart_model', 'cm');
            $this->load->model('product_model', 'pm');

            $proimg = $this->pm->get_img($proid, true);

            $this->cm->insert_cart($proid, $proimg->img);
        }

        $this->_header('', 'cart');
    }

    public function del_from_cart() {

        $proid = $this->uri->segment(2) ? $this->uri->segment(2) : '';

        $this->load->model('cart_model', 'cm');

        if ($proid)
            $this->cm->cartdelete($proid);

        header("location:" . $_SERVER['HTTP_REFERER']);

        exit;
    }

    public function changeqty() {

        $this->load->model('cart_model', 'cm');

        $this->cm->changeqty();

        $all_session = $this->session->all_userdata();

        $total = 0;

        foreach ($all_session['product'] as $k => $v) {

            $total += $v['qty'] * $v['product_price'];
        }

        echo $total * $all_session['currency']->rate;
    }

    public function shipaddress() {

        $this->load->model('comm_model', 'cm');
        $this->load->library('myclass');
        $this->load->library('encrypt');

        $ssinfo = $this->session->all_userdata();

        $uid = array_key_exists('userinfo', $ssinfo) ? $ssinfo['userinfo']->mid : '';

        if ($uid) {

            $site_info = $this->cm->get_siteinfo();

            $data['countrys'] = $this->cm->get_countrys();

            $get_addr = array(
                'siteid' => $site_info->siteid,
                'uid' => $uid
            );

            $get_key = $this->myclass->get_key($site_info->siteid);

            $the_data = $this->encrypt->encode(json_encode($get_addr), $get_key);

            $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;

            $httpCode = '';

            $res = $this->myclass->vpost(self::$url . "get_address", $send_info, $httpCode);

            if ($httpCode['http_code'] == 200) {

                $result = json_decode($res);

                $data['shipaddr'] = unserialize($result->shipaddr);
                $data['shipaddr'] = unserialize($result->shipaddr);
            }
        }

        $this->_header('shipaddress', 'step1', $data, 1, '', 'shipaddress');
    }

    public function payment() {

        $this->load->model('comm_model', 'cm');
        $this->load->library('myclass');
        $site_info = $this->cm->get_siteinfo();
        $post_addr = $this->input->post();
        $this->session->set_userdata('shipaddr', $post_addr);

        $this->_header('payment', 'payment', '', 1);
    }

    public function do_pay() {
        //$this->load->model('comm_model', 'cm');

        $this->load->library('myclass');

        $this->load->library('encrypt');


        $product_cart = $this->session->all_userdata();
        //var_dump($product_cart);die(0);
        $site_info = $this->comm_model->get_siteinfo();

        $shipaddr = $billaddr = $product_cart['shipaddr'];
        $userinfo = array_key_exists('userinfo', $product_cart) ? $product_cart['userinfo'] : "";
        $uid = $userinfo->mid;
       

        if ($uid) {
            $post_data = array(
                'uid' => $uid,
                'shiptype' => '',
                'shippingfee' => 0,
                'currency' => '',
                'siteid' => $site_info->siteid,
                'message' => '',
                'shipaddr' => serialize($shipaddr),
                'billaddr' => serialize($billaddr),
                'status' => 'unpaid',
                 'ordersn' => date("YmdHis") . str_pad(rand(0, 999), 3, 0)
            );
           
            $post_data['total'] = 0;
           
            foreach ($product_cart['product'] as $key => $val) {
                $post_data['total'] += $val['qty'] * $val['price'];
                $post_data['goodslist'][] = array(
                    'product_id' => $val['pro_id'],
                    'product_name' => $val['proname'],
                    'price' => $val['price'],
                    'qty' => $val['qty'],
                    'img' => $val['img'],
                    'product_url' => $val['prourl']
                );
                $product_name = $val["proname"];
            }

            $get_key = $this->myclass->get_key($site_info->siteid);
            $the_data = $this->encrypt->encode(json_encode($post_data), $get_key);
            $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
            $httpCode = '';

            $res = $this->myclass->vpost(self::$url . "add_order", $send_info, $httpCode);
           // var_dump($res);die(0);		
            
            if ($httpCode['http_code'] == 200) {
                $result = json_decode($res);
            //    echo 0.3;
                if ($result->ack == 0) {
              //      echo 1;
                    $send_data = array(
                        'orderid' => $result->orderid,
                        'shippingfee' => '30',
                        'product' => $product_cart['product'],
                        'amount' => $post_data['total']
                    );
                    $this->session->unset_userdata('product');
                    $payinfo = $this->myclass->get_payinfo($send_data);
                    //$this->_header('waiting','posti',$payinfo,1);
                  //  echo 2;
                    $merchantno = '361100';
                    /* MD5私钥 */
                    $MD5key = self::$key;
                    /* 订单号 */
                    $BillNo = $post_data['ordersn'];
                    /* 订单金额 */
                    $Amount = $post_data['total'];
                    /* 速汇通开通币种
                      对应的币种有   2:欧元  1:美元  6:日元  4:英镑  5:港币   7:澳元  11:加元  8:挪威克朗 3:人民币  12:丹麦克朗  13:瑞典克朗
                     */
                    $CurrencyArray = array('EUR' => '2', 'USD' => 'USD', 'JPY' => '6', 'GBP' => '4', 'AUD' => '7', 'CAD' => '11', 'NOK' => '8', 'RMB' => '3', 'DKK' => '12', 'HKD' => '5', 'SEK' => '13');
                    /* 支付币种 */
                    $Currency = $CurrencyArray['USD'];
                    /* 支付币种符号 */
                   // $CurrencyName = $_POST['CurrencyName'];

                    /* 返回地址[必填]返回数据给商户，商户自己填写，需要在商户后台绑定 */
                    $ReturnURL = 'http://' . $_SERVER['HTTP_HOST'] . '/return_url';
                    /* 通知地址；为确保数据的正常返回  暂时不可用， 可不填写 */
                    $NoticeURL = 'http://' . $_SERVER['HTTP_HOST'] . '/return_page';
                    /* 备注信息[选填]填写网站的域名，方便以后维护,可不做修改 */
                    $Remark = 'http://' . $_SERVER['HTTP_HOST'];
                    $Language = "en";

                    $MD5src = $BillNo . "487570085" . $Currency . $post_data['total'] . $MD5key;
                    $MD5info = md5($MD5src);
                    $shipfee = $post_data["shippingfee"];
                   // var_dump($product_cart);
                    $send_data = array(
                        'product_name[0]' => $product_name,
                        "merchantno" => $merchantno,
                        "siteid" => "487570085",
                        "order_sn" => $BillNo,
                        "iver" => "3.14",
                        "version" => "zencart1.3.9",
                        "embed" => "Y",
                        "rpcookie" => "rpma9pjml53l6blkmu64281enh04",
                        "order_time" => date("Y-m-d H:i:s", time()),
                        "language" => $Language,
                        "currency" => $Currency,
                        "amount" => $post_data['total'],
                        "shippingfee" => $shipfee,
                        "vat" => "0.00",
                        "discount" => "0.00",
                        "product_no[0]" => "",
                        "price_unit[0]" => $post_data['total'],
                        "quantity[0]" => "1",
                        "returnurl" => $ReturnURL,
                        "notifyurl" => $NoticeURL,
                        "verifycode" => $MD5info,
                        'shipfirstname' => $product_cart['shipaddr']['firstname'],
                        'shiplastname' => $product_cart['shipaddr']['lastname'],
                        'shipaddress' => $product_cart['shipaddr']['address'],
                        'shipcity' => $product_cart['shipaddr']['city'],
                        'shipcountry' => $product_cart['shipaddr']['country'],
                        'shipstate' => $product_cart['shipaddr']['state'],
                        'shippostcode' => $product_cart['shipaddr']['postcode'],
                        'email' => $product_cart['userinfo']->email,
                        'shipphone' => $product_cart['shipaddr']['phone'],
                        'billfirstname' => $product_cart['shipaddr']['firstname'],
                        'billlastname' => $product_cart['shipaddr']['lastname'],
                        'billaddress' => $product_cart['shipaddr']['address'],
                        'billcity' => $product_cart['shipaddr']['city'],
                        'billcountry' => $product_cart['shipaddr']['country'],
                        'billstate' => $product_cart['shipaddr']['state'],
                        'billpostcode' => $product_cart['shipaddr']['postcode'],
                        'billphone' => $product_cart['shipaddr']['phone']
                    );
                   // var_dump($send_data);
                    //$this->session->unset_userdata('product');

                    //$dataaa['posti'] = $this->comm_model->do_pay($send_data);
                    $dataaa['posti'] = $send_data;
                   
                    $this->session->set_userdata('dataaa', $dataaa);
                    $this->load->view('posti', $dataaa);
                }
            }
        }
    }
    public function refresh_pay() {
        $data = $this->session->userdata("dataaa");
        $this->load->view("refresh", $data);
    }
    
    function object_array($array) {
        if (is_object($array)) {
            $array = (array) $array;
        }
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }

    public function continue_pay() {
        
         $this->load->model('comm_model');
        $site_info = $this->comm_model->get_siteinfo();
        $data['site_info'] = $site_info;
        
        $orderid = $this->uri->segment(2) ? $this->uri->segment(2) : '';
        
        $site_id = $data['site_info']->siteid;
        $showproduct = array(
            'orderid' => $orderid
        );
       
        $get_key = $this->get_key();

        /*
        $the_data = $this->encrypt->encode(json_encode($showproduct), $get_key);

        $send_info = 'the_data=' . $the_data . '&siteid=' . $site_id;
       
        $res = $this->myclass->vpost(self::$url . "product_list", $send_info);
        
        if ($res) {
         * 
         */
            // echo $res;
        /*
            $result = json_decode($res, true);
            
            foreach ($result as $key => $val) {

                $re[$key] = $val;
            }
         * 
         */

              $get_key = $this->get_key();
              $the_data = $this->encrypt->encode(json_encode($showproduct), $get_key);
              $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
              $httpCode = '';
              $res2 = $this->myclass->vpost(self::$url . "order_info", $send_info, $httpCode);
              $result2 = json_decode($res2);
             // var_dump($result2);
              $order_info = $this->object_array($result2->orderinfo);
             // var_dump($order_info);die(0);
           // $ses_info = $this->session->all_userdata();
              
            //$currinfo = array();

          //  $currinfo = $this->comm_model->get_currency($order_info['currency']);
       
            /*
            foreach ($re as $k => $v) {

                $re[$k]['symbol'] = $currinfo->symbol;

                $re[$k]['price'] = $re[$k]['price'];
            }
             * 
             */
             
            //$data['products'] = $re;

            $order_info['shipaddr'] = unserialize($order_info['shipaddr']);

            $order_info['billaddr'] = unserialize($order_info['billaddr']);

            $data['order_info'] = $order_info;
            //var_dump($data);die(0);
        //}
       // var_dump($data);die(0);
        $merchantno = '361100';
        /* MD5私钥 */
        $MD5key = self::$key;
        /* 订单号 */
        $BillNo = $order_info['ordersn'];
        /* 订单金额 */
        $Amount = $order_info['total'];
        /* 速汇通开通币种
          对应的币种有   2:欧元  1:美元  6:日元  4:英镑  5:港币   7:澳元  11:加元  8:挪威克朗 3:人民币  12:丹麦克朗  13:瑞典克朗
         */
        $CurrencyArray = array('EUR' => '2', 'USD' => 'USD', 'JPY' => '6', 'GBP' => '4', 'AUD' => '7', 'CAD' => '11', 'NOK' => '8', 'RMB' => '3', 'DKK' => '12', 'HKD' => '5', 'SEK' => '13');
        /* 支付币种 */
        $Currency = $order_info['currency'];
        /* 支付币种符号 */
        $CurrencyName = $order_info['currency'];

        /* 返回地址[必填]返回数据给商户，商户自己填写，需要在商户后台绑定 */
        $ReturnURL = 'http://' . $_SERVER['HTTP_HOST'] . '/return_url';
        /* 通知地址；为确保数据的正常返回  暂时不可用， 可不填写 */
        $NoticeURL = 'http://' . $_SERVER['HTTP_HOST'] . '/return_page';
        /* 备注信息[选填]填写网站的域名，方便以后维护,可不做修改 */
        $Remark = 'http://' . $_SERVER['HTTP_HOST'];
        $Language = "en";

        $MD5src = $BillNo . "487570085" . $Currency . $order_info['total'] . $MD5key;
        $MD5info = md5($MD5src);
        $send_data = array(
            'product_name[0]' => "product",
            "merchantno" => $merchantno,
            "siteid" => "487570085",
            "order_sn" => $BillNo,
            "iver" => "3.14",
            "version" => "zencart1.3.9",
            "embed" => "Y",
            "rpcookie" => "rpma9pjml53l6blkmu64281enh04",
            "order_time" => date("Y-m-d H:i:s", time()),
            "language" => "en",
            "currency" => $Currency,
            "amount" => $order_info['total'],
            "shippingfee" => $order_info["shippingfee"],
            "vat" => "0.00",
            "discount" => "0.00",
            "product_no[0]" => "",
            "price_unit[0]" => $order_info['total'],
            "quantity[0]" => "1",
            "returnurl" => $ReturnURL,
            "notifyurl" => $NoticeURL,
            "verifycode" => $MD5info,
            'shipfirstname' => $order_info['shipaddr']['firstname'],
            'shiplastname' => $order_info['shipaddr']['lastname'],
            'shipaddress' => $order_info['shipaddr']['address'],
            'shipcity' => $order_info['shipaddr']['city'],
            'shipcountry' => $order_info['shipaddr']['country'],
            'shipstate' => $order_info['shipaddr']['state'],
            'shippostcode' => $order_info['shipaddr']['postcode'],
            'email' => $order_info['email'],
            'shipphone' => $order_info['shipaddr']['phone'],
            'billfirstname' => $order_info['billaddr']['firstname'],
            'billlastname' => $order_info['billaddr']['lastname'],
            'billaddress' => $order_info['billaddr']['address'],
            'billcity' => $order_info['billaddr']['city'],
            'billcountry' => $order_info['billaddr']['country'],
            'billstate' => $order_info['billaddr']['state'],
            'billpostcode' => $order_info['billaddr']['postcode'],
            'billphone' => $order_info['billaddr']['phone']
        );

        $this->session->unset_userdata('product');

        //$dataaa['posti'] = $this->comm_model->do_pay($send_data);
        $dataaa['posti'] = $send_data;

        $this->session->set_userdata('dataaa', $dataaa);
        $this->load->view('posti', $dataaa);
    }
    
    public function return_url() {
        if ($userinfo = $this->session->userdata('userinfo')) {

            $data['username'] = $userinfo['firstname'] . ' ' . $userinfo['lastname'];

            $uid = $userinfo['mid'];

            $email = $userinfo['email'];
        }

        $post_data = $this->input->get();

        $OrderId = $post_data['_order_sn'];
        $verified = $post_data["_verified"];
        $Status = $post_data['_verified'] == 'approved' ? 'paid' : $post_data['_verified'];

        $Status = $Status == 'test approve' ? 'test' : $Status;

        if ($Status != 'test' && $Status != 'paid') {
            $Status = "unpaid";
        }

        $site_info = $this->comm_model->get_siteinfo();

        $siteid = $site_info['siteid'];

        if ($Status == "Success" || $Status == 'test') {
            $post_info = array(
                'ordersn' => $OrderId,
                'siteid' => $siteid,
                "succ" => 1,
                "email" => $email
            );
            $data["msg"] = "Your payment has gone through successfully.";
        } else {
            $post_info = array(
                'ordersn' => $OrderId,
                'siteid' => $siteid,
                "succ" => 0,
                "email" => $email
            );
            $data["msg"] = "Pay fail!";
        }
        $get_key = $this->get_key();

        $the_data = $this->encrypt->encode(json_encode($post_info), $get_key);

        $send_info = 'the_data=' . $the_data . '&siteid=' . $siteid;

        $re = $this->myclass->vpost(self::$url . "order_status/", $send_info);


        $Signature = $post_data['_verifycode'];
        $currency = $post_data['_currency'];
        $amount = $post_data['_total'];
        $transactionid = $post_data['_transactionid'];
        $key = self::$key;

        $site_info = $this->comm_model->get_siteinfo();
        $siteid = $post_data["_siteid"];
        $Signature2 = MD5($OrderId . $siteid . $currency . $amount . $transactionid . $verified . $key);

        if ($Signature != $Signature2) {
            echo "签名错误";
            die();
        }

        $site_info = $this->comm_model->get_siteinfo();

        $siteid = $site_info['siteid'];

        $post_info = array(
            'ordersn' => $OrderId,
            'status' => $Status,
            'transactionid' => $transactionid
        );

        $get_key = $this->get_key();

        $the_data = $this->encrypt->encode(json_encode($post_info), $get_key);

        $send_info = 'the_data=' . $the_data . '&siteid=' . $siteid;

        $ss = $this->myclass->vpost(self::$url . "update_order_status/", $send_info);

        //var_dump($ss);
        //echo "Pay success";die(0);

        $this->load->view('pay_succ', $data);
    }

    public function return_page() {

        $post_data = $this->input->post();

        $OrderId = $post_data['_order_sn'];
        $Status = $post_data['_verified'] == 'approved' ? 'Success' : $post_data['_verified'];
        $Status = $Status == 'test approve' ? 'test' : $Status;
        $Signature = $post_data['_verifycode'];
        $currency = $post_data['_currency'];
        $amount = $post_data['_total'];
        $transactionid = $post_data['_transactionid'];
        $key = self::$key;

        $site_info = $this->comm_model->get_siteinfo();
        $siteid = "487570085";

        $Signature2 = MD5($OrderId . $siteid . $currency . $amount . $transactionid . $Status . $key);

        if ($Signature != $Signature2) {
            echo "签名错误";
            die();
        }

        $site_info = $this->comm_model->get_siteinfo();

        $siteid = $site_info['siteid'];

        $post_info = array(
            'orderid' => $OrderId,
            'status' => $Status,
            'transactionid' => $transactionid
        );

        $get_key = $this->get_key();

        $the_data = $this->encrypt->encode(json_encode($post_info), $get_key);

        $send_info = 'the_data=' . $the_data . '&siteid=' . $siteid;

        $ss = $this->myclass->vpost(self::$url . "update_order_status/", $send_info);

        //var_dump($ss);
        echo "支付成功";
        die(0);
    }
    public function user_mag() {

        $this->load->model('comm_model', 'cm');
        $this->load->library('myclass');
        $this->load->library('encrypt');

        $ssinfo = $this->session->all_userdata();
        //var_dump($ssinfo);exit;

        if (array_key_exists('userinfo', $ssinfo)) {
            $uid = $ssinfo['userinfo']->mid;
        } else {
            $this->_header('', '', '', 1);
        }

        $action = $this->uri->segment(2) ? $this->uri->segment(2) : '';
        $num = $this->uri->segment(3) ? $this->uri->segment(3) : 1;
        $site_info = $this->cm->get_siteinfo();

        switch ($action) {

            case 'orderdetail':

                $showproduct = array(
                    'orderid' => $num
                );

                $get_key = $this->myclass->get_key($site_info->siteid);
                $the_data = $this->encrypt->encode(json_encode($showproduct), $get_key);
                $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
                $httpCode = '';
                $res = $this->myclass->vpost(self::$url . "order_info", $send_info, $httpCode);
                if ($httpCode['http_code'] == 200) {
                    $result = json_decode($res);

                    $result->orderinfo->shipaddr = unserialize($result->orderinfo->shipaddr);
                    $result->orderinfo->billaddr = unserialize($result->orderinfo->billaddr);
                   // var_dump($result);
                    //die(0);
                    $this->_header('orderdetail', 'user_orderdetail', $result, 1);
                }
                break;

            case 'myorder':

                $showorder = array(
                    'uid' => $uid,
                    'index' => $num,
                    'pagesize' => '10',
                    'siteid' => $site_info->siteid
                );

                $get_key = $this->myclass->get_key($site_info->siteid);
                $the_data = $this->encrypt->encode(json_encode($showorder), $get_key);
                $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
                $httpCode = '';
                $res = $this->myclass->vpost(self::$url . "order_list", $send_info, $httpCode);
                if ($httpCode['http_code'] == 200) {
                    $result = json_decode($res);
                    $total_page = $result->page;
                    $size = $showorder['pagesize'];
                    $pagination = $this->myclass->pagination($num, $total_page / $size, '/user_mag/myorder');
                    $this->_header('myorder', 'user_orderlist', $result->order, 1, $pagination);
                }
                break;

            case 'addresslist':

                $get_addr = array(
                    'siteid' => $site_info->siteid,
                    'uid' => $uid
                );
                $get_key = $this->myclass->get_key($site_info->siteid);
                $the_data = $this->encrypt->encode(json_encode($get_addr), $get_key);
                $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
                $httpCode = '';
                $res = $this->myclass->vpost(self::$url . "get_address", $send_info, $httpCode);
                if ($httpCode['http_code'] == 200) {
                    $result = json_decode($res);
                    $data['shipaddr'] = unserialize($result->shipaddr);
                    $data['billaddr'] = unserialize($result->billaddr);
                    $data['countrys'] = $this->cm->get_countrys();
                    $this->_header('useraddress', 'user_addressmodify', $data, 1);
                }
                break;

            case 'account':

                $this->_header('account', 'user_password', '', 1);
                break;

            case 'question':

                $get_question = array(
                    'siteid' => $site_info->siteid,
                    'uid' => $uid
                );
                $get_key = $this->myclass->get_key($site_info->siteid);
                $the_data = $this->encrypt->encode(json_encode($get_question), $get_key);
                $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
                $httpCode = '';
                $res = $this->myclass->vpost(self::$url . "get_question_list", $send_info, $httpCode);
                if ($httpCode['http_code'] == 200) {
                    $result = json_decode($res);
                    $this->_header('userquestion', 'user_question', $result, 1);
                }
                break;

            default:

                $showorder = array(
                    'uid' => $uid,
                    'index' => $num,
                    'pagesize' => '10',
                    'siteid' => $site_info->siteid
                );

                $get_key = $this->myclass->get_key($site_info->siteid);
                $the_data = $this->encrypt->encode(json_encode($showorder), $get_key);
                $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
                $httpCode = '';
                $res = $this->myclass->vpost(self::$url . "order_list", $send_info, $httpCode);
                if ($httpCode['http_code'] == 200) {
                    $result = json_decode($res);
                    $total_page = $result->page;
                    $size = $showorder['pagesize'];
                    $pagination = $this->myclass->pagination($num, $total_page / $size, '/user_mag/myorder');
                    $this->_header('myorder', 'user_orderlist', $result->order, 1, $pagination);
                }
        }
    }

    public function updata_pword() {

        $this->load->model('comm_model', 'cm');
        $this->load->library('myclass');
        $this->load->library('encrypt');

        $ssinfo = $this->session->all_userdata();
        if (array_key_exists('userinfo', $ssinfo)) {
            $uid = $ssinfo['userinfo']->mid;
        } else {
            $this->_header('', '', '', 1);
        }
        $site_info = $this->cm->get_siteinfo();
        $post_data = $this->input->post();
        $post_info = array(
            'uid' => $uid,
            'newpassword' => md5($post_data['newpwd']),
            'oldpassword' => md5($post_data['password'])
        );
        $get_key = $this->myclass->get_key($site_info->siteid);
        $the_data = $this->encrypt->encode(json_encode($post_info), $get_key);
        $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
        $httpCode = '';
        $res = $this->myclass->vpost(self::$url . "update_password", $send_info, $httpCode);
        if ($httpCode['http_code'] == 200) {
            $result = json_decode($res);
            if ($result->ack == 0) {
                $re = 'update success!';
                $this->_header('userpass', 'user_password', $re, 1);
            } else {
                $re = 'update fail!';
                $this->_header('userpass', 'user_password', $re, 1);
            }
        }
    }

    public function addresssave() {

        $this->load->model('comm_model', 'cm');
        $this->load->library('myclass');
        $this->load->library('encrypt');
        $ssinfo = $this->session->all_userdata();
        if (array_key_exists('userinfo', $ssinfo)) {
            $uid = $ssinfo['userinfo']->mid;
        } else {
            $this->_header('', '', '', 1);
        }
        $site_info = $this->cm->get_siteinfo();
        $post_info = $this->input->post();
        $shipaddr = array(
            'firstname' => $post_info['firstname'],
            'lastname' => $post_info['lastname'],
            'address' => $post_info['address'],
            'city' => $post_info['city'],
            'state' => $post_info['state'],
            'postcode' => $post_info['postcode'],
            'country' => $post_info['country'],
            'phone' => $post_info['phone']
        );
        $modaddr = array(
            'uid' => $uid,
            'siteid' => $site_info->siteid,
            'shipaddr' => serialize($shipaddr),
            'billaddr' => serialize($shipaddr)
        );
        $get_key = $this->myclass->get_key($site_info->siteid);
        $the_data = $this->encrypt->encode(json_encode($modaddr), $get_key);
        $send_info = 'the_data=' . $the_data . '&siteid=' . $site_info->siteid;
        $httpCode = '';
        $res = $this->myclass->vpost(self::$url . "update_address", $send_info, $httpCode);
        if ($httpCode['http_code'] == 200) {
            $result = json_decode($res);
            if ($result->ack == 0) {
                $re = 'update success!';
                $this->_header('useraddress', 'user_addressmodify', $re, 1);
            } else {
                $re = 'update fail!';
                $this->_header('useraddress', 'user_addressmodify', $re, 1);
            }
        }
    }

    public function about() {

        $this->_header('about', 'about');
    }

    public function shipping() {

        $this->_header('shipping', 'shipping');
    }

    public function warranty() {

        $this->_header('warranty', 'warranty');
    }

    public function privacypolic() {

        $this->_header('privacypolic', 'privacypolic');
    }

    public function contactus() {

        $this->_header('contactus', 'contactus');
    }

    public function makes() {

        $this->load->model('product_model', 'pm');

        $data = array('pid' => 1);

        $re = $this->pm->get_makes($data);

        $this->_header('makes', 'makes', $re);
    }

    public function charge() {

        $make_id = $this->uri->segment(2) ? $this->uri->segment(2) : '';
        $partnumberurl = $this->uri->segment(3) ? $this->uri->segment(3) : '';
        $year = $this->uri->segment(4) ? $this->uri->segment(4) : '';
        $model = $this->uri->segment(5) ? $this->uri->segment(5) : '';

        $data = array();

        if ($make_id && $make_id != 'makes')
            $data['main_make_id'] = $make_id;
        if ($partnumberurl && $partnumberurl != 'partnumber')
            $data['partnumberurl'] = $partnumberurl;
        if ($year && $year != 'years')
            $data['year'] = $year;
        if ($model && $model != 'models')
            $data['model'] = $model;

        //var_dump($data);exit;

        if ($data) {

            $this->load->model('product_model', 'pm');
            $proinfo = $this->pm->get_info('products', $data, 0, 6);

            foreach ($proinfo as $k => $v) {

                $proimg = $this->pm->get_img($v->pro_id, true);
                $proinfo[$k]->img = $proimg->img;
            }

//				var_dump($proinfo);exit;

            if ($make_id && $make_id != 'makes')
                $makes = $make_id;
            else
                $makes = $this->pm->getmakes($data);
            if ($partnumberurl && $partnumberurl != 'partnumber')
                $partnumberurl = $partnumberurl;
            else
                $partnumberurl = $this->pm->get_partnumber($data);
            if ($year && $year != 'years')
                $year = $year;
            else
                $year = $this->pm->get_year($data);
            if ($model && $model != 'models')
                $model = $model;
            else
                $model = $this->pm->get_model($data);

            if ($partnumberurl || $year || $model) {

                $data = array(
                    'makes' => $makes,
                    'partnumberurl' => $partnumberurl,
                    'year' => $year,
                    'model' => $model,
                    'proinfo' => $proinfo
                );

                //var_dump($data);exit;
                $this->_header('category', 'charge', $data);
            } else {
                echo "404";
                die(0);
                $this->_header('404', '404');
            }
        } else {
            echo "405";
            die(0);
            $this->_header('404', '404');
        }
    }

    public function nonepage() {

        $this->_header('404', '404');
    }

    public function _header($title, $template = '', $tdata = '', $islogin = 0, $pagination = '', $repage = '') {

        $user = $this->session->all_userdata();

        if ($islogin == 1) {

            if (empty($user['userinfo']->email)) {
                if ($repage)
                    $this->session->set_userdata('repage', $repage);

                header("Location:/login");
                exit;
            }
        }

        if (array_key_exists('userinfo', $user)) {

            $data['user'] = $user['userinfo'];
        } else {

            $data['user'] = '';
        }

        if ($title) {

            $data['title'] = $title;
        } else {

            $this->load->model('comm_model');
            $site_info = $this->comm_model->get_siteinfo();
            $data['title'] = $site_info->title;
        }

        if ($products = array_key_exists('product', $user) ? $user['product'] : '') {

            $data['products'] = $products;
            $data['item'] = count($products);
        } else {

            $data['products'] = '';
            $data['item'] = 0;
        }

        if (!empty($tdata)) {
            $data['dt'] = $tdata;
        } else {
            $data['dt'] = '';
        }

        if (!empty($pagination))
            $data['page'] = $pagination;

        //var_dump($data);exit;

        $this->assign('data', $data);

        $this->assign('site_url', site_url());

        $this->display('header.php');
        $this->display($template . '.php');
        $this->display('footer.php');
    }

}
